﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Butcher.winform.EnterpriseForms.WhiteNoteList;
using static Butcher.winform.EnterpriseForms.WhiteNoteUpdate;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms.MessSalguhterPut
{
    public partial class WeighingScaleShowUpdata : Form
    {


        public string kl1;
        public string kl2;
            public string kl3;

        public WeighingScaleShowUpdata()
        {
            InitializeComponent();
            WeighingScaleShow lists = new WeighingScaleShow();



            GETback();




        }


        public string Guid { get; set; }

        /// <summary>
        /// 副产称重修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            var getid = WeighingScaleShow.getid;
            string url = @"http://localhost:5229/api/Batch/ReturnWeighingUpdate";
            HttpClient client = new HttpClient();
            NEWUPdates list = new NEWUPdates();
            list.ByproductId = getid;
            list.DocumentsNo = comboBox1.SelectedIndex.ToString();
            list.ByproductDate = Convert.ToDateTime(textBox6.Text);
            list.ShoppingType = comboBox2.Text;
            list.byproductNum = Convert.ToInt32(textBox4.Text);
            list.byproductNoteWeight = Convert.ToInt32(textBox2.Text);
            list.byproductNoteTare = Convert.ToInt32(textBox3.Text);
            list.Rremark = textBox5.Text;
            var content = new StringContent(JsonSerializer.Serialize<NEWUPdates>(list), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["code"];

            if (results != null)
            {
                MessageBox.Show("修改成功");
                this.Close();
            }

        }




        /// <summary>
        /// 赋值
        /// </summary>
        private void GETback()
        {

            int kl = WeighingScaleShow.getid;
            string url = @"http://localhost:5229/api/Batch/Handle";
            HttpClient client = new HttpClient();
            GETids list = new GETids();
            list.XWeightid = kl;

            var content = new StringContent(JsonSerializer.Serialize<GETids>(list), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["date"];

            comboBox1.Text = results["documentsNo"].ToString();
            comboBox2.Text = results["shoppingName"].ToString();
            textBox1.Text = results["byproductNoteWeight"].ToString();
            kl1= results["byproductNoteWeight"].ToString();
            textBox2.Text = results["byproductNoteTare"].ToString();
            kl2 = results["byproductNoteTare"].ToString();
           
            textBox3.Text = results["byproductNetWeight"].ToString();
            kl3 = results["byproductNetWeight"].ToString();

            textBox4.Text = results["byproductNum"].ToString();
            textBox5.Text = results["rremark"].ToString();
            textBox6.Text = DateTime.Now.ToString();
            Guid = results["stashid"].ToString();
        }










        public class NEWUPdates
        {

            /// <summary>
            /// 副产ID
            /// </summary>
            public int ByproductId { get; set; }

            /// <summary>
            /// 批宰计划号
            /// </summary>
            public string DocumentsNo { get; set; }
            /// <summary>
            /// 副产称重时间
            /// </summary>
            public DateTime ByproductDate { get; set; }
            /// <summary>
            /// 商品分类
            /// </summary>

            public string ShoppingType { get; set; }
            /// <summary>
            /// 称重头数
            /// </summary>
            public int byproductNum { get; set; }
            /// <summary>
            /// 毛重
            /// </summary>
            public decimal byproductNoteWeight { get; set; }
            /// <summary>
            /// 皮重
            /// </summary>
            public decimal byproductNoteTare { get; set; }
            /// <summary>
            /// 净重
            /// </summary>
            public decimal byproductNetWeight { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string Rremark { get; set; }
        }


        public class GETids
        {

            public int XWeightid { get; set; }


        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            var gh = Convert.ToInt32(textBox4.Text == "" ? 0 : textBox4.Text);

            if(gh==0)
            {
                textBox1.Text = kl1;
                textBox2.Text = kl2;
                textBox3.Text =kl3;
            }
            else
            {
                textBox1.Text = (Convert.ToInt32(textBox1.Text) * gh).ToString();
                textBox2.Text = (Convert.ToInt32(textBox2.Text) * gh).ToString();
                textBox3.Text = (Convert.ToInt32(textBox3.Text) * gh).ToString();
            }
           
        }
    }
}
